package com.example.month11.vp_tab;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.example.month11.vp_tab.view.RotateTransform;
import com.example.month11.vp_tab.view.ScaleTransformer;
import com.example.recyclerview01.R;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class BannerActivity extends AppCompatActivity {
    private ViewPager mVpMain;
    private int[] mResIds = new int[]{
            0xffff0000,
            0xff00ff00,
            0xff0000ff,
            0xffff8000
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banner);

        initViews();
    }

    private void initViews() {
        mVpMain = findViewById(R.id.vp_main);
        mVpMain.setOffscreenPageLimit(3);
         mVpMain.setPageMargin(40); // 间隔：dp recommended!!!
        mVpMain.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return mResIds.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                View view = new View(container.getContext());
                view.setBackgroundColor(mResIds[position]);
                ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);
                view.setLayoutParams(lp);
                container.addView(view);
                return view;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }
        });
        // 动画效果
        mVpMain.setPageTransformer(true, new RotateTransform());
    }

}
